﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Concrete;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        private readonly IProductRepository _productRepository;

        public ProductController(IProductRepository productRepository)
        {
            _productRepository = productRepository;
            PageSize = 4;
        }

        public int PageSize { get; set; }

        // GET: Product
        public ViewResult List(string category,int page=1)
        {
            var viewModel = new ProductsListViewModel();
            viewModel.Products = _productRepository.Products.Where(p=>p.Category==category||category==null).Skip(PageSize * (page - 1)).Take(PageSize);
            viewModel.PageInfo = new PageInfo
            {
                CurrentPage = page,
                TotalItems = _productRepository.Products.Count(p => p.Category==category||category==null),
                ItemsPerPage = PageSize,
               
            };
            viewModel.CurrentCategory = category;
            return View(viewModel);
        }

        public FileContentResult GetImage(int productId)
        {
            var product = _productRepository.Products.FirstOrDefault(p=>p.ProductID==productId);
            if (product!=null)
            {
                return File(product.ImageData, product.ImageMimeType);
            }
            return null;
        }
    }
}